package com.example.demo.controller.admin;

import com.example.demo.Util.Result;
import com.example.demo.Util.ResultUtil;
import com.example.demo.mapper.CollegeMap;
import com.example.demo.po.College;
import com.example.demo.po.User;
import com.example.demo.service.CollegeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/college")
public class CollegeController {

    @Autowired
    CollegeMap collegeMap;

    @Autowired
    CollegeService collegeService;

    @GetMapping("/get")
    @ResponseBody
    public Result GetCollege(){
        List<College> collegeList = new ArrayList();
        collegeList = collegeMap.GetCollege();
        return ResultUtil.success(collegeList);
    }

    @GetMapping("/search")
    @ResponseBody
    public Result SearchCollege( @RequestParam("name") String name){
        System.out.println(name);
        if(name == null||name ==""){
            return ResultUtil.success(collegeMap.GetCollege());
        }

        List<College> collegeList = new ArrayList();
        String n = "%"+name+"%";
        collegeList = collegeMap.GetCollegeByName(n);
        System.out.println(collegeList);
        return ResultUtil.success(collegeList);
    }

    @PostMapping("/save")
    @ResponseBody
    public Result SaveCollege(@RequestBody College college){



       Result result = collegeService.SaveCollege(college);
        return result;
    }

    @GetMapping("/delete/{id}")
    public Result DeleteCollege(@PathVariable int id){

        if(id == -1){
            return ResultUtil.fail("参数错误");
        }

        collegeMap.DeleteCollege(id);

        return ResultUtil.success("删除成功");
    }

    @PostMapping("/update")
    public Result UpdateCollege(@RequestBody College college){

        if(college == null){
            return ResultUtil.fail("参数错误");
        }

        return collegeService.UpdateCollege(college);
    }

}
